/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO_BUS;

import DataObject.MyRowData;
import java.util.Vector;
import org.w3c.dom.Element;

/**
 *
 * @author MinhNhat
 */
public class MyExpression {

    String LeftSide;
    String RightSide;
    String Operator;
    String Relationship;
    public String OperatorModel[] = {"!=",">=","<=","=","<>",">", "<", "not like", "like"};

    public MyExpression(String left, String right, String operator)
    {
        this.LeftSide = left;
        this.RightSide = right;
        this.Operator = operator;
    }
    public MyExpression(String strExpress) {
        strExpress = strExpress.trim();
        for (int i = 0; i < OperatorModel.length; i++) {
            int indexOfOperator = strExpress.indexOf(OperatorModel[i]);
            if (indexOfOperator != -1) {
                Operator = OperatorModel[i];
                String[] core = strExpress.split(Operator);
                if (core.length == 2) {
                    core[0]=core[0].replace("\"","'");
                    core[1]=core[1].replace("\"","'");
                    LeftSide= core[0].trim();
                    RightSide=core[1].trim();
                    Relationship ="";
                }
                break;
            }
        }
    }

    public void Print() {
        System.out.println(LeftSide + " " + Operator + " " + RightSide + " " + Relationship);
    }

    public boolean isRight(Element ele) {
        
        if(LeftSide==null)
        {
            return false;
        }
        
        if (!ele.hasAttribute(LeftSide)) {
            return false;
        }
        String strLeftSide = ele.getAttribute(LeftSide);
        if (Operator.compareTo("=") == 0) {
            if (strLeftSide.compareTo(RightSide.replace("'", "").trim()) == 0) {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        if (Operator.compareTo("<>") == 0 || Operator.compareTo("!=") == 0) {
            if (strLeftSide.compareTo(RightSide.replace("'", "").trim()) != 0) {
                return true;
            }
            else
            {
                return false;
            }
        }

        if(Operator.compareTo("<")==0)
        {
            if(MySentence.isCharacter(RightSide))
            {
                return false;
            }
            else
            {
                int left,right;
                left= Integer.parseInt(strLeftSide);
                right= Integer.parseInt(RightSide);
                if(left<right)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }   
        }
        
        if(Operator.compareTo("<=")==0)
        {
            if(MySentence.isCharacter(RightSide))
            {
                return false;
            }
            else
            {
                int left,right;
                left= Integer.parseInt(strLeftSide);
                right= Integer.parseInt(RightSide);
                if(left<=right)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }   
        }
        
        if(Operator.compareTo(">")==0)
        {
            if(MySentence.isCharacter(RightSide))
            {
                return false;
            }
            else
            {
                int left,right;
                left= Integer.parseInt(strLeftSide);
                right= Integer.parseInt(RightSide);
                if(left>right)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }   
        }
        
        if(Operator.compareTo(">=")==0)
        {
            if(MySentence.isCharacter(RightSide))
            {
                return false;
            }
            else
            {
                int left,right;
                left= Integer.parseInt(strLeftSide);
                right= Integer.parseInt(RightSide);
                if(left>=right)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }   
        }
        
        if (Operator.compareTo("like") == 0) {
            String temp = RightSide.replace("'", "").trim();
            if (!temp.contains("%")) {
                if (temp.length() != strLeftSide.length()) {
                    return false;
                }

                if (temp.contains("_")) {
                    for (int i = 0; i < temp.length(); i++) {
                        if (temp.charAt(i) != '_') {
                            if (temp.charAt(i) != strLeftSide.charAt(i)) {
                                return false;
                            }
                        }
                    }
                    return true;
                } else {
                    if (strLeftSide.compareTo(temp) == 0) {
                        return true;
                    }
                }
                return false;
            }

            String core = temp.substring(temp.indexOf("%") + 1, temp.lastIndexOf("%"));
            if (strLeftSide.contains(core)) {
                return true;
            } else {
                return false;
            }

        }

        if (Operator.compareTo("not like") == 0) {
            String temp = RightSide.replace("'", "").trim();
            if (!temp.contains("%")) {
                if (temp.length() != strLeftSide.length()) {
                    return true;
                }

                if (temp.contains("_")) {
                    for (int i = 0; i < temp.length(); i++) {
                        if (temp.charAt(i) != '_') {
                            if (temp.charAt(i) != strLeftSide.charAt(i)) {
                                return true;
                            }
                        }
                    }
                    return false;
                } else {
                    if (strLeftSide.compareTo(temp) == 0) {
                        return false;
                    }
                }
                return true;
            }

            String core = temp.substring(temp.indexOf("%") + 1, temp.lastIndexOf("%"));
            if (strLeftSide.contains(core)) {
                return false;
            } else {
                return true;
            }

        }
        return false;
    }
    
    public boolean isRight(MyRowData Row) {
        
        if(LeftSide==null)
        {
            return false;
        }
        
        if (!LeftSide.contains("'")&&!Row.hasColumn(LeftSide)) {
            return false;
        }
        
        if(!RightSide.contains("'")&& !Row.hasColumn(RightSide))
        {
            return false;
        }
        
        String strLeftSide,strRightSide;
        strLeftSide = strRightSide = "";
        
        if(!LeftSide.contains("'"))
        {
            strLeftSide = Row.GetData(LeftSide).Value;
        }
        else
        {
            strLeftSide=LeftSide.replace("'", "");
        }
        
        if(!RightSide.contains("'"))
        {
            strRightSide = Row.GetData(RightSide).Value;
        }
        else
        {
            strRightSide = RightSide.replace("'", "");
        }
        
        if (Operator.compareTo("=") == 0) {           
            if (strLeftSide.compareTo(strRightSide) == 0) {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        if (Operator.compareTo("<>") == 0 || Operator.compareTo("!=") == 0) {
            if (strLeftSide.compareTo(strRightSide) != 0) {
                return true;
            }
            else
            {
                return false;
            }
        }

        if(Operator.compareTo("<")==0)
        {
            if(MySentence.isCharacter(strRightSide) || MySentence.isCharacter(strLeftSide))
            {
                return false;
            }
            else
            {
                int left,right;
                left= Integer.parseInt(strLeftSide);
                right= Integer.parseInt(strRightSide);
                if(left<right)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }   
        }
        
        if(Operator.compareTo("<=")==0)
        {
            if(MySentence.isCharacter(strRightSide) || MySentence.isCharacter(strLeftSide))
            {
                return false;
            }
            else
            {
                int left,right;
                left= Integer.parseInt(strLeftSide);
                right= Integer.parseInt(strRightSide);
                if(left<=right)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }   
        }
        
        if(Operator.compareTo(">")==0)
        {
            if(MySentence.isCharacter(strRightSide) || MySentence.isCharacter(strLeftSide))
            {
                return false;
            }
            else
            {
                int left,right;
                left= Integer.parseInt(strLeftSide);
                right= Integer.parseInt(strRightSide);
                if(left>right)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }   
        }
        
        if(Operator.compareTo(">=")==0)
        {
            if(MySentence.isCharacter(strRightSide) || MySentence.isCharacter(strLeftSide))
            {
                return false;
            }
            else
            {
                int left,right;
                left= Integer.parseInt(strLeftSide);
                right= Integer.parseInt(strRightSide);
                if(left>=right)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }   
        }
        
        if (Operator.compareTo("like") == 0) {         
            if (!strRightSide.contains("%")) {
                if (strRightSide.length() != strLeftSide.length()) {
                    return false;
                }

                if (strRightSide.contains("_")) {
                    for (int i = 0; i < strRightSide.length(); i++) {
                        if (strRightSide.charAt(i) != '_') {
                            if (strRightSide.charAt(i) != strLeftSide.charAt(i)) {
                                return false;
                            }
                        }
                    }
                    return true;
                } else {
                    if (strLeftSide.compareTo(strRightSide) == 0) {
                        return true;
                    }
                }
                return false;
            }

            String core = strRightSide.substring(strRightSide.indexOf("%") + 1, strRightSide.lastIndexOf("%"));
            if (strLeftSide.contains(core)) {
                return true;
            } else {
                return false;
            }

        }

        if (Operator.compareTo("not like") == 0) {
            if (!strRightSide.contains("%")) {
                if (strRightSide.length() != strLeftSide.length()) {
                    return true;
                }

                if (strRightSide.contains("_")) {
                    for (int i = 0; i < strRightSide.length(); i++) {
                        if (strRightSide.charAt(i) != '_') {
                            if (strRightSide.charAt(i) != strLeftSide.charAt(i)) {
                                return true;
                            }
                        }
                    }
                    return false;
                } else {
                    if (strLeftSide.compareTo(strRightSide) == 0) {
                        return false;
                    }
                }
                return true;
            }

            String core = strRightSide.substring(strRightSide.indexOf("%") + 1, strRightSide.lastIndexOf("%"));
            if (strLeftSide.contains(core)) {
                return false;
            } else {
                return true;
            }

        }
        return false;
    }

    public static boolean CheckCondition(Vector Conditions) {

        
        return false;
    }

    public static int GetTheTruth(Vector Conditions, Element ele) {
        if (Conditions.isEmpty()) {
            return 1;
        }
        MyExpression Expression = (MyExpression) Conditions.get(0);

        int resultMine;
        if (Expression.isRight(ele)) {
            resultMine = 1;
        } else {
            resultMine = 0;
        }
        Conditions.remove(0);
        if (Expression.Relationship.trim().equalsIgnoreCase("and")) {
            return resultMine * GetTheTruth(Conditions, ele);
        }

        if (Expression.Relationship.trim().equalsIgnoreCase("or")) {
            return resultMine + GetTheTruth(Conditions, ele);
        }
        return resultMine;

    }

    public static int GetTheTruth(Vector Conditions, MyRowData Row) {
        if (Conditions.isEmpty()) {
            return 1;
        }
        MyExpression Expression = (MyExpression) Conditions.get(0);

        int resultMine;
        if (Expression.isRight(Row)) {
            resultMine = 1;
        } else {
            resultMine = 0;
        }
        Conditions.remove(0);
        if (Expression.Relationship.trim().equalsIgnoreCase("and")) {
            return resultMine * GetTheTruth(Conditions, Row);
        }

        if (Expression.Relationship.trim().equalsIgnoreCase("or")) {
            return resultMine + GetTheTruth(Conditions, Row);
        }
        return resultMine;

    }
    
    public MyTable GetLeftTable(Vector Tables) {
        String[] parts = LeftSide.replace(".", " ").split(" ");
        if (parts.length > 0) {
            return MyTable.GetTableByName(Tables, parts[0]);
        }
        return null;
    }

    public MyTable GetRightTable(Vector Tables) {
        String[] parts = RightSide.replace(".", " ").split(" ");
        if (parts.length > 0) {

            return MyTable.GetTableByName(Tables, parts[0]);
        }
        return null;
    }

    /*  public static int GetTheTruthForSelectSentence(Vector Conditions,Vector Tables)
    {
    if(Conditions.size()==0)
    {
    return 1;
    }
    MyExpression Expression= (MyExpression)Conditions.get(0);
    
    int resultMine;
    MyTable tableLeft = Expression.GetLeftTable(Tables);
    MyTable tableRight = Expression.GetRightTable(Tables);
    if(Expression.isRightForSelectSentence(tableLeft,tableRight))
    {
    resultMine=1;
    }
    else
    {
    resultMine=0;
    }    
    Conditions.remove(0);
    if(Expression.Relationship.equalsIgnoreCase("and"))
    {
    return resultMine * GetTheTruthForSelectSentence(Conditions,Tables);
    }
    
    if(Expression.Relationship.equalsIgnoreCase("or"))
    {
    return resultMine + GetTheTruthForSelectSentence(Conditions,Tables);
    }
    return 1;
    
    }
     */
    public static int GetTheTruth(Vector Conditions, Vector Datas, Vector Vars) {
        if (Conditions.isEmpty()) {
            return 1;
        }
        MyExpression Expression = (MyExpression) Conditions.get(0);

        int resultMine;
        if (Expression.isRight(Datas, Vars)) {
            resultMine = 1;
        } else {
            resultMine = 0;
        }
        Conditions.remove(0);
        if (Expression.Relationship.trim().equalsIgnoreCase("and")) {
            return resultMine * GetTheTruth(Conditions, Datas, Vars);
        }

        if (Expression.Relationship.trim().equalsIgnoreCase("or")) {
            return resultMine + GetTheTruth(Conditions, Datas, Vars);
        }

        return resultMine;

    }

    public boolean isRight(Vector Datas, Vector Type) {
        int indexOfTable;
        if (LeftSide.contains(".") && RightSide.contains("'")) {
            String[] parts = LeftSide.replace(".", " ").split(" ");
            if (parts.length == 2) {
                indexOfTable = GetIndexOfTable(Type, parts[0]);
            } else {
                indexOfTable = GetIndexOfTable(Type, parts[0] + "." + parts[1]);
            }

            Element row = (Element) Datas.get(indexOfTable);
            MyExpression temp = new MyExpression(parts[parts.length - 1],RightSide,Operator);      
            return temp.isRight(row);
        }

        if (RightSide.contains(".") && LeftSide.contains("'")) {

            String[] parts = RightSide.replace(".", " ").split(" ");
            if (parts.length == 2) {
                indexOfTable = GetIndexOfTable(Type, parts[0]);
            } else {
                indexOfTable = GetIndexOfTable(Type, parts[0] + "." + parts[1]);
            }

            Element row = (Element) Datas.get(indexOfTable);
            MyExpression temp = new MyExpression(parts[parts.length - 1],LeftSide,Operator);
            return temp.isRight(row);
        }

        if (RightSide.contains(".") && LeftSide.contains(".")) {
            String valueLeft, valueRight;
            String[] parts = LeftSide.replace(".", ",").split(",");
            if (parts.length == 2) {
                indexOfTable = GetIndexOfTable(Type, parts[0]);
            } else {
                indexOfTable = GetIndexOfTable(Type, parts[0] + "." + parts[1]);
            }

            Element rowLeft = (Element) Datas.get(indexOfTable);
            valueLeft= rowLeft.getAttribute(parts[parts.length - 1]);

            parts = RightSide.replace(".", ",").split(",");
            if (parts.length == 2) {
                indexOfTable = GetIndexOfTable(Type, parts[0]);
            } else {
                indexOfTable = GetIndexOfTable(Type, parts[0] + "." + parts[1]);
            }

            Element rowRight = (Element) Datas.get(indexOfTable);
            valueRight = rowRight.getAttribute(parts[parts.length - 1]);
           
            if (valueLeft.compareTo(valueRight) == 0) {
                return true;
            } else {
                return false;
            }
        }
        
        if(LeftSide.contains(".") && !RightSide.contains("'"))
        {
            String[] parts = LeftSide.replace(".", " ").split(" ");
            if (parts.length == 2) {
                indexOfTable = GetIndexOfTable(Type, parts[0]);
            } else {
                indexOfTable = GetIndexOfTable(Type, parts[0] + "." + parts[1]);
            }

            Element row = (Element) Datas.get(indexOfTable);
            
            MyExpression temp = new MyExpression(parts[parts.length - 1],RightSide,Operator);
            return temp.isRight(row);
        }
        return false;
    }

    public int GetIndexOfTable(Vector Type, String str) {
        for (int i = 0; i < Type.size(); i++) {
            String type = (String) Type.get(i);
            if (type.compareTo(str) == 0) {
                return i;
            }
        }
        return -1;
    }
}
